<?php
namespace app\applet\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class HousingInformation extends Model
{
    protected $table = 'housing_information';
    /**
     * 住房管理
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function housingList(){
        //单元楼数据
        $data=Db::name('unit_building')
            ->field('tung_num,unit_num,layer_num,floor_type')
            ->where(['u_type'=>1])
            ->order('tung_num asc,unit_num asc')
            ->select();
        if($data!=null){
            foreach($data as $k=>$v){
                $data[$k]['unit']=$v['tung_num'].'-'.$v['unit_num'];
            }
        }
        //后台朝向数据
        $date=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->field('housing_oorientation')
            ->find();
        //住房配套
        $i=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->field('housing_matching')
            ->find();
        //模板
        $m=Db::name('housing_template')
            ->field('template_id,te_name')
            ->where(['te_state'=>1])
            ->select();
        //经纪人
        $user=Db::name('admin_user')
            ->where(['admin_status'=>1])
            ->field('admin_id,name')
            ->select();
        //var_dump($m,$date);exit;
        //判断设置没有   没有返回2
        if($data!=null && $date!=null && $i!=null){
            $arr['orientation']=$date['housing_oorientation'];//朝向
            $arr['unit']=$data;//单元
            $arr['matching']=$i['housing_matching'];//配套
            $arr['user']=$user;//配套
            $arr['template']=$m;//配套
            return $arr;
        }else{
            return 2;
        }
    }
    /**
     * 住房房源添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function housingAdd($data){
        //处理数据
        $arr['h_tung_num'] = isset($data['h_tung_num']) ? trim($data['h_tung_num']) : "";
        $arr['h_layer_num'] = isset($data['h_layer_num']) ? trim($data['h_layer_num']) : "";
        $arr['h_agent'] = isset($data['h_agent']) ? trim($data['h_agent']) : "";
        $arr['h_room_number'] = isset($data['h_room_number']) ? trim($data['h_room_number']) : "";
        $arr['h_unit_num'] = isset($data['h_unit_num']) ? trim($data['h_unit_num']) : "";
        $arr['h_area'] = isset($data['h_area']) ? trim($data['h_area']) : "";
        $arr['h_root'] = isset($data['h_root']) ? trim($data['h_root']) : "";
        $arr['h_guest_room'] = isset($data['h_guest_room']) ? trim($data['h_guest_room']) : "";
        $arr['h_toilet'] = isset($data['h_toilet']) ? trim($data['h_toilet']) : "";
        $arr['h_floor_plans'] = isset($data['h_floor_plans']) ? implode(',',$data['h_floor_plans']) : "";
        $arr['h_indoor_drawings'] = isset($data['h_indoor_drawings']) ? implode(',',$data['h_indoor_drawings']) : "";
        $arr['h_money'] = isset($data['h_money']) ? trim($data['h_money']) : "";
        $arr['h_orientation'] = isset($data['h_orientation']) ? $data['h_orientation'] : "";
        $arr['h_matching'] = isset($data['h_matching']) ? implode(',',$data['h_matching']) : "";
        $arr['h_a_time'] = time();
       $datas=Db::name('housing_information')->insert($arr);
        //判断是否存为模板
        $data['h_template']= isset($data['h_template']) ? trim($data['h_template']) : "";
        if($data['h_template']==1){
            //处理数据
            //添加操作用户id
            /*$uid=get_user_id($data['token']);
            $count['te_uid'] = $uid;*/
            $count['te_uid'] = 2;
            $count['te_name'] = isset($data['h_template_name']) ? trim($data['h_template_name']) : "";
            $count['te_area'] = isset($data['h_area']) ? trim($data['h_area']) : "";
            $count['te_money'] = isset($data['h_money']) ? trim($data['h_money']) : "";
            $count['te_root'] = isset($data['h_root']) ? trim($data['h_root']) : "";
            $count['te_guest_room'] = isset($data['h_guest_room']) ? trim($data['h_guest_room']) : "";
            $count['te_toilet'] = isset($data['h_toilet']) ? trim($data['h_toilet']) : "";
            $count['te_orientation'] = isset($data['h_orientation']) ? trim($data['h_orientation']) : "";
            $count['te_floor_plans'] = isset($data['h_floor_plans']) ? implode(',',$data['h_floor_plans']) : "";
            $count['te_indoor_drawings'] = isset($data['h_indoor_drawings']) ? implode(',',$data['h_indoor_drawings']) : "";
            $count['te_matching'] = isset($data['h_matching']) ? implode(',',$data['h_matching']) : "";
            $count['te_a_time'] = time();
         //   var_dump($count);exit;
            $data=Db::name('housing_template')->insert($count);
            //var_dump($count);exit;
        }
        return $datas;
    }
    /**
     * 判断楼层房号是否重复
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tungs($data){
        //处理数据
        $i['h_state'] = 1;
        $where['h_tung_num'] = $data['h_tung_num'];
        $where['h_unit_num'] = $data['h_unit_num'];
        $where['h_layer_num'] = $data['h_layer_num'];
        $where['h_room_number'] = $data['h_room_number'];
        $date=Db::name('housing_information')->where($i)->where($where)->find();
        return $date;
    }
    /**
     * 判断楼层房号是否重复
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tungsUp($data){
        //处理数据
        $i['h_state'] = 1;
        $where['h_tung_num'] = $data['h_tung_num'];
        $where['h_unit_num'] = $data['h_unit_num'];
        $where['h_layer_num'] = $data['h_layer_num'];
        $where['h_room_number'] = $data['h_room_number'];
        $wheres['housing_id'] = array('neq',$data['housing_id']);
        $date=Db::name('housing_information')->where($i)->where($wheres)->where($where)->find();
        return $date;
    }
    /**
     * 住房资源查询
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function housingOne($id){
        //处理数据
        $data=Db::name('housing_information')
            ->alias('a')
            ->join('admin_user','a.h_agent=admin_user.admin_id')
            ->where(['housing_id'=>$id])
            ->where(['h_state'=>1])
            ->find();
        $data['h_floor_plans']=explode(',',$data['h_floor_plans']);
        $data['h_indoor_drawings']=explode(',',$data['h_indoor_drawings']);
        $data['h_matching']=explode(',',$data['h_matching']);
        return $data;
    }
    /**
     * 住房修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitUpdate($data){
        //处理数据
        $where['housing_id']=isset($data['housing_id']) ? trim($data['housing_id']) : "";
        $arr['h_agent'] = isset($data['h_agent']) ? trim($data['h_agent']) : "";
        $arr['h_tung_num'] = isset($data['h_tung_num']) ? trim($data['h_tung_num']) : "";
        $arr['h_unit_num'] = isset($data['h_unit_num']) ? trim($data['h_unit_num']) : "";
        $arr['h_layer_num'] = isset($data['h_layer_num']) ? trim($data['h_layer_num']) : "";
        $arr['h_money'] =  isset($data['h_money']) ? trim($data['h_money']) : "";
        $arr['h_room_number'] = isset($data['h_room_number']) ? trim($data['h_room_number']) : "";
        $arr['h_area'] = isset($data['h_area']) ? trim($data['h_area']) : "";
        $arr['h_root'] = isset($data['h_root']) ? trim($data['h_root']) : "";
        $arr['h_guest_room'] = isset($data['h_guest_room']) ? trim($data['h_guest_room']) : "";
        $arr['h_toilet'] = isset($data['h_toilet']) ? trim($data['h_toilet']) : "";
        $arr['h_floor_plans'] = isset($data['h_floor_plans']) ? implode(',',$data['h_floor_plans']) : "";
        $arr['h_indoor_drawings'] = isset($data['h_indoor_drawings']) ? implode(',',$data['h_indoor_drawings']) : "";
        $arr['h_orientation'] = isset($data['h_orientation']) ? trim($data['h_orientation']) : "";
        $arr['h_matching'] = isset($data['h_matching']) ? implode(',',$data['h_matching']) : "";
        $arr['h_u_time'] = time();
        //var_dump($arr);exit;
        $data=Db::name('housing_information')->where($where)->update($arr);
        return $data;
    }
    /**
     * 住房房源显示
     * 1 智能排序， 2 价格从低到高， 3 价格从高到低
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function index($order, $house_shape, $page){
        switch ($order) {
            case 1:
                $data = $this->index_intelligent_sorting($house_shape, $page);
                return $data;
            case 3:
                $data = $this->index_price_escalation($house_shape, $page);
                return $data;
            case 2:
                $data = $this->index_price_decline($house_shape, $page);
                return $data;
            default:
                return;
        }
    }
    /**
     * 住房房源显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function housingDel($data){
        $date['housing_id']=$data['housing_id'];
        $data=Db::name('housing_information')
            ->where($date)
            ->update(['h_state'=>2]);
        return $data;
    }

    /**
     * 获取详情
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id) {
        $where = [
            'h_state' => '1',
            'housing_id' => $id
        ];
        $data=Db::name($this->table)
            ->field('housing_id, h_tung_num, h_unit_num, h_layer_num, h_money, h_area, h_root
            , h_guest_room, h_toilet, h_floor_plans, h_indoor_drawings, h_matching, h_agent, h_room_number,create_id')
            ->where($where)
            ->find();
        return $data;
    }

    /**
     * 智能排序
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function index_intelligent_sorting($house_shape, $page) {
        $where=[];
        $where['h.h_state']=1;
        $where['h.lease']=1;
        $where['a.admin_status']=1;
        if (!empty($house_shape)) {
            $house_shape = explode(',', $house_shape);
            $where['h.h_root']=$house_shape[0];
            $where['h.h_guest_room']=$house_shape[1];
            $where['h.h_toilet']=$house_shape[2];
        }
        $count=Db::name('housing_information')
            ->alias('h')
            ->join('admin_user a', 'h.create_id=a.admin_id')
            ->where($where)
            ->count();
        $data=Db::name('housing_information')
            ->page($page, get_list_rows())
            ->alias('h')
            ->field('h.housing_id,h.h_tung_num,h.h_area,h.h_unit_num,h.h_layer_num,h.h_room_number,h.h_root,h.h_money,h.h_guest_room,h.h_toilet,h.h_orientation,h.h_agent,h.h_indoor_drawings, h.h_floor_plans, h.create_id')
            ->join('admin_user a', 'h.create_id=a.admin_id')
            ->where($where)
            ->order("h.h_tung_num asc,h.h_unit_num asc,h.h_layer_num asc,h.h_room_number asc")
            ->select();

        foreach($data as $k=>$v){
            $data[$k]['unit']=$v['h_tung_num'].'-'.$v['h_unit_num'].'-'.$v['h_layer_num'].'-'.$v['h_room_number'];
            $data[$k]['room']=$v['h_root'].'室'.$v['h_guest_room'].'厅'.$v['h_toilet'].'卫';
            //$data['count']=$count;
        }
        $date['data']=$data;
        $date['count']=$count;

        $model = new IntentionalHouse();
        foreach ($date['data'] as $k => $value) {
            $count_info = $model->getOneCount(1, $value['housing_id']);
            $date['data'][$k]['intentional'] = $count_info;
        }

        $last_names = array_column($date['data'],'intentional');
        array_multisort($last_names,SORT_DESC,$date['data']);

        // $data['count']=$count;
        return $date;
    }

    /**
     * 价格从高到低
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function index_price_decline($house_shape, $page) {
        $where=[];
        $where['h.h_state']=1;
        $where['h.lease']=1;
        $where['a.admin_status']=1;
        if (!empty($house_shape)) {
            $house_shape = explode(',', $house_shape);
            $where['h.h_root']=$house_shape[0];
            $where['h.h_guest_room']=$house_shape[1];
            $where['h.h_toilet']=$house_shape[2];
        }
        $count=Db::name('housing_information')
            ->alias('h')
            ->join('admin_user a', 'h.create_id=a.admin_id')
            ->where($where)
            ->count();
        $data=Db::name('housing_information')
            ->alias('h')
            ->field('h.housing_id,h.h_tung_num,h.h_area,h.h_unit_num,h.h_layer_num,h.h_room_number,h.h_root,h.h_money,h.h_guest_room,h.h_toilet,h.h_orientation,h.h_agent,h.h_indoor_drawings, h.h_floor_plans, h.create_id')
            ->join('admin_user a', 'h.create_id=a.admin_id')
            ->where($where)
            ->page($page, get_list_rows())
            ->order("h.h_money desc")
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['unit']=$v['h_tung_num'].'-'.$v['h_unit_num'].'-'.$v['h_layer_num'].'-'.$v['h_room_number'];
            $data[$k]['room']=$v['h_root'].'室'.$v['h_guest_room'].'厅'.$v['h_toilet'].'卫';
            //$data['count']=$count;
        }
        $date['data']=$data;
        $date['count']=$count;
        // $data['count']=$count;
        return $date;
    }

    /**
     * 价格从低到高
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function index_price_escalation($house_shape, $page) {
        $where=[];
        $where['h.h_state']=1;
        $where['h.lease']=1;
        $where['a.admin_status']=1;
        if (!empty($house_shape)) {
            $house_shape = explode(',', $house_shape);
            $where['h.h_root']=$house_shape[0];
            $where['h.h_guest_room']=$house_shape[1];
            $where['h.h_toilet']=$house_shape[2];
        }
        $count=Db::name('housing_information')
            ->alias('h')
            ->join('admin_user a', 'h.create_id=a.admin_id')
            ->where($where)
            ->count();
        $data=Db::name('housing_information')
            ->alias('h')
            ->field('h.housing_id,h.h_tung_num,h.h_area,h.h_unit_num,h.h_layer_num,h.h_room_number,h.h_root,h.h_money,h.h_guest_room,h.h_toilet,h.h_orientation,h.h_agent,h.h_indoor_drawings, h.h_floor_plans, h.create_id')
            ->join('admin_user a', 'h.create_id=a.admin_id')
            ->where($where)
            ->page($page, get_list_rows())
            ->order("h.h_money asc")
            ->select();
        foreach($data as $k=>$v){
            $data[$k]['unit']=$v['h_tung_num'].'-'.$v['h_unit_num'].'-'.$v['h_layer_num'].'-'.$v['h_room_number'];
            $data[$k]['room']=$v['h_root'].'室'.$v['h_guest_room'].'厅'.$v['h_toilet'].'卫';
            //$data['count']=$count;
        }
        $date['data']=$data;
        $date['count']=$count;
        // $data['count']=$count;
        return $date;
    }
}